package cn.longge.mapper;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.util.StringUtil;

import java.util.Map;

public class WorkSqlProvider {

    public String selectByCondition(Map<String,Object> params){
        return new SQL(){
            {
                SELECT("wo.id,wo.code,wo.`status`,wo.create_date,wo.update_date,co.name as create_office_name," +
                        "cu.name as create_user_name,tu.name as transport_user_name,ru.name as resipient_user_name ");
                FROM("work_order wo ");
                LEFT_OUTER_JOIN("sys_user cu \n" +
                        "on wo.create_user_id = cu.id  ");
                LEFT_OUTER_JOIN(" sys_user tu \n" +
                        "on wo.transport_user_id = tu.id ");
                LEFT_OUTER_JOIN("sys_user ru \n" +
                        "on wo.recipient_user_id = ru.id");
                LEFT_OUTER_JOIN("sys_office co\n" +
                        "on cu.office_id = co.id ");
                WHERE("wo.del_flag = 0");
                if(params.containsKey("fromTime") && !StringUtils.isEmpty(params.get("fromTime"))){
                    WHERE("wo.create_date >= #{fromTime}");
                }
                if(params.containsKey("toTime") && !StringUtils.isEmpty(params.get("toTime"))){
                    WHERE("wo.create_date <= #{toTime}");
                }
                if(params.containsKey("status") && !StringUtils.isEmpty(params.get("status"))){
                    WHERE("wo.status = #{status}");
                }
                if(params.containsKey("officeId") && !StringUtils.isEmpty(params.get("officeId"))){
                    WHERE("cu.office_id = #{officeId} or tu.office_id = #{officeId} or ru.office_id = #{officeId}");
                }

            }
        }.toString();
    }
}
